package com.app.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.AbstractMap.SimpleEntry;
import java.util.Iterator;
import java.util.WeakHashMap;

public class DoubleLevelWeakHashMap<K, I, V> {
	private WeakHashMap<K, WeakHashMap<I, V>> mHash;

	public DoubleLevelWeakHashMap() {
		mHash = new WeakHashMap<K, WeakHashMap<I, V>>();
	}

	public void putValue(K key, I id, V value) {
		if (mHash.get(key) == null || !mHash.get(key).equals(id)) {
			WeakHashMap<I, V> iMap = new WeakHashMap<I, V>();
			iMap.put(id, value);
			mHash.put(key, iMap);
		} else
			mHash.get(key).put(id, value);

	}

	public Iterator<V> getValues(K key) {
		WeakHashMap<I, V> iMap = mHash.get(key);
		if(iMap==null)
			return new EmptyIterator();
		Iterator<V> iterator = iMap.values().iterator();
		return iterator;
	}

	public V getValue(K key, I id) {
		WeakHashMap<I, V> iMap = mHash.get(key);
		return iMap.get(id);
	}

	public void removeValues(K key) {
		mHash.remove(key);
	}

	public void removeValue(K key, I id) {
		WeakHashMap<I, V> iMap = mHash.get(key);
		iMap.remove(id);

	}
	private class EmptyIterator implements Iterator<V>{

		@Override
		public boolean hasNext() {
			return false;
		}

		@Override
		public V next() {
			// DO NOTHING 
			return null;
		}

		@Override
		public void remove() {
			// DO NOTHING 
			
		}}
}